import { _store } from './state.js';

export const cartPrice = () => {
  let total = 0;

  for (const x in _store.cartList) {
    if (!Object.prototype.hasOwnProperty.call(_store.cartList, x)) continue;
    const { num, price } = _store.cartList[x];
    if (num > 0) {
      total += Number(price) * num;
    }
  }

  return Number(Number(total).toFixed(1));
};

export const cartTotalPrice = () => {
  const _cartPrice = cartPrice();
  if (_cartPrice === 0) return 0;
  const price = _cartPrice + deliveryPrice();
  return Number(Number(price).toFixed(1));
};

export const deliveryPrice = () => {
  const { first, sec, fee = 1 } = _store.deliveryInfo;

  let price = 0;

  if (first) {
    price = Number(first) + sec;
  } else if (fee) {
    price = fee;
  }

  return Number(Number(price).toFixed(1));
};
